#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

class Solution {
    public:
        int removeDuplicates(int A[], int n) {
            int index = 0;
            int count = 0;
            map<int, int> myMap;
            for (int i = 0; i < n; i++) {
                if (myMap.count(A[i]) == 0) {
                    myMap[A[i]] = 1;
                    A[index ] =A[i];
                    index++;
                    count++;
                } else if (myMap[A[i]] < 2) {
                    myMap[A[i]]++;
                    A[index ]=A[i];
                    index++;
                    count++;
                } 
            }
            return count;
        }
};

//add better code
        if (!n) return 0;
        int index = 0, i = 0, cnt;
        while (i < n) {
            A[index] = A[i];
            cnt = 1;
            i++;
            while(A[i] == A[index] && i < n) {if (++cnt <= 2) A[++index] = A[i]; i++;}
            index++;
        }   
        return index;


int main(int argc, char **argv)
{
    Solution mySolution;
    int A[] = {1,1,1,2,2,3};

    cout << "-----------------Test 1--------------------" << endl;
    mySolution.removeDuplicates(A, sizeof(A)/sizeof(int));


    cout << "-----------------Test 2--------------------" << endl;


    cout << "-----------------Test 3--------------------" << endl;


    cout << "-----------------Test 4--------------------" << endl;


    cout << "-----------------Test 5--------------------" << endl;



}
